/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#pragma once

#define DEFAULT_BUFFER_SIZE 64
#define DEFAULT_BUFFER_DELTA 32

#include <iostream>
#include "utildll.h"

namespace greenhills {
namespace util {

/**
 * @brief Memory buffer class for use with std::iostream
 *
 * This class is not fully implemented. It does NOT support seek and tell
 * functionality for example.
 */
class GH_UTIL_STD_CLASS memstreambuf : public std::basic_streambuf<char, std::char_traits<char> >
{
public:
    /**
     * @brief Creates empty buffer with given initial size, and increment
     *
     * @param pInitialSize initial buffer size (64 bytes by default)
     * @param pIncrement increment of buffer size (32 bytes by default)
     */
    memstreambuf(unsigned int pInitialSize = DEFAULT_BUFFER_SIZE,
                 unsigned int pIncrement = DEFAULT_BUFFER_DELTA);

    memstreambuf(const char *pData, unsigned int pLength);

    ~memstreambuf();

public:
        /**
     * @brief Returns buffer data
         *
     * @return pointer to buffer data
         */
    const char *data() const;

        /**
     * @brief Returns current buffer size
         *
         * @return buffer length in bytes
         */
    unsigned int length() const;

private:
    virtual int_type overflow(int_type c);
    void realloc(unsigned int pNewSize);

private:
    unsigned int mIncrement;
    unsigned int mAllocatedSize;
    char *mData;
};

} //namespace util
} //namespace greenhills
